package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.KnowledgeTypeRelationVo;
import com.zb.entity.SelectGoodsVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjVideo;
import com.zb.extend.KnowledgeTypeVideoExtend;
import com.zb.mapper.ZjVideoMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjKnowledgeTypeVideoService;
import com.zb.service.ZjVideoContentService;
import com.zb.service.ZjVideoService;
import com.zb.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品视频表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-31
 */
@Service
public class ZjVideoServiceImpl extends ServiceImpl<ZjVideoMapper, ZjVideo> implements ZjVideoService {
    @Resource
    private ZjVideoMapper videoMapper;
    @Resource
    private ZjVideoContentService videoContentService;
    @Resource
    private ZjKnowledgeTypeVideoService knowledgeTypeVideoService;

    // 新增或修改视频
    @Override
    @Transactional
    public void save(Long custAccId, VideoVo videoVo) {
        Assert.isTrue(null == videoMapper.existByName(custAccId, videoVo.getId(), videoVo.getName()), "视频名称已重复", videoVo.getName());
        ZjVideo video = new ZjVideo();
        BeanUtils.copyProperties(videoVo, video);
        video.setDisabled(videoVo.getStatus());
        if (null != video.getId()) {
            this.updateById(video);
        } else {
            video.setId(IdWorker.getId());
            video.setCustAccId(custAccId);
            video.setClickNum(0);
            this.save(video);
        }
        videoContentService.save(custAccId, video.getId(), videoVo.getVideoPaths(), videoVo.getDelVideoPaths());
        knowledgeTypeVideoService.save(custAccId, video.getId(), videoVo.getKnowledgeTypeVos(), videoVo.getDelRelationIds());
    }

    // 获取视频列表
    @Override
    public Map<String, Object> getList(Long custAccId, VideoSelectVo selectVo) {
        QueryWrapper<ZjVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.orderByDesc("gmt_update");
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjVideo> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<KnowledgeTypeVideoExtend> videoExtendList = knowledgeTypeVideoService.getByVideoIds(page.getRecords().stream().map(ZjVideo::getId).toList());

        List<VideoListVo> voList = page.getRecords().stream().map(t -> {
            VideoListVo vo = VideoListVo.beanToVo(t);
            vo.setKnowledgeType(videoExtendList.stream().filter(f -> Objects.equals(f.getVideoId(), t.getId())).map(KnowledgeTypeVideoExtend::getName).collect(Collectors.joining("、")));
            return vo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 获取视频信息
    @Override
    public VideoVo getInfo(Long id) {
        ZjVideo video = this.getById(id);
        Assert.isTrue(null != video, "["+id+"]数据不存在！", null);

        VideoVo vo = new VideoVo();
        BeanUtils.copyProperties(video, vo);
        vo.setStatus(video.getDisabled());
        vo.setVideoPaths(videoContentService.getImageByVidelId(id).stream().map(imgVo -> {
            VideoPathVo pathVo = new VideoPathVo();
            pathVo.setId(imgVo.getId());
            pathVo.setPath(imgVo.getImage());
            return pathVo;
        }).toList());
        vo.setKnowledgeTypeVos(knowledgeTypeVideoService.getByVideoId(id).stream().map(type -> {
            return new KnowledgeTypeRelationVo(type.getId(), type.getKnowledgeTypeId());
        }).toList());
        return vo;
    }

    // 批量启用或禁用视频
    @Override
    public void updateBatchDisableByIds(List<Long> idList, boolean disable) {
        videoMapper.updateBatchDisableByIds(idList, disable);
    }

    // 根据视频ID集合查询名称
    @Override
    public List<String> getNameByIds(List<Long> idList) {
        if (null == idList || idList.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjVideo> wrapper = new QueryWrapper<>();
        wrapper.select("name");
        wrapper.in("id", idList);
        return this.list(wrapper).stream().map(ZjVideo::getName).toList();
    }

}
